/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CompareFilesForm.java
 *
 * Created on Apr 19, 2009, 9:37:41 PM
 */

package forms;

import core.XItem;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hung Cuong <nhc.hcmuns at gmail.com>
 */
public class CompareFilesForm extends javax.swing.JFrame {

    private String _pathLeft = "";
    private String _pathRight = "";
    /** Creates new form CompareFilesForm */
    public CompareFilesForm(String pathLeft, String pathRight) {
        initComponents();
        _pathLeft = pathLeft;
        _pathRight = pathRight;
        try {
            txtLeft.setText(XItem.getFileContent(pathLeft));
            txtRight.setText(XItem.getFileContent(pathRight));
            compare();
        } catch (IOException ex) {
            Logger.getLogger(CompareFilesForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void compare() throws IOException {
        BufferedReader reader1 = new BufferedReader(new FileReader(_pathLeft));
        BufferedReader reader2 = new BufferedReader(new FileReader(_pathRight));

        String line1,line2 = null;
        int totalDiff = 0;
        while((line1 = reader1.readLine()) != null && (line2 = reader2.readLine()) != null) {
            int len1 = line1.length();
            int len2 = line2.length();
            int len = len1 > len2 ? len1 : len2;
            for(int i = 0; i < len; ++i) {
                if(line1.charAt(i) != line2.charAt(i)) {
                    totalDiff ++;
                }
            }
        }
        reader1.close();
        reader2.close();

        File file1 = new File(_pathLeft);
        File file2 = new File(_pathRight);
        totalDiff += Math.abs(file1.length() - file2.length());
        
        String result = "File 1: " + _pathLeft + "\r\n" +
                        "File 2: " + _pathRight + "\r\n" +
                        "\r\n" +
                        "Number of differences: " + totalDiff;
        txtResult.setText(result);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtResult = new javax.swing.JTextArea();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtLeft = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtRight = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tiny Total Commander");

        txtResult.setColumns(20);
        txtResult.setEditable(false);
        txtResult.setRows(3);
        jScrollPane1.setViewportView(txtResult);

        jSplitPane1.setResizeWeight(0.5);

        txtLeft.setColumns(20);
        txtLeft.setRows(5);
        jScrollPane2.setViewportView(txtLeft);

        jSplitPane1.setLeftComponent(jScrollPane2);

        txtRight.setColumns(20);
        txtRight.setRows(5);
        jScrollPane3.setViewportView(txtRight);

        jSplitPane1.setRightComponent(jScrollPane3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextArea txtLeft;
    private javax.swing.JTextArea txtResult;
    private javax.swing.JTextArea txtRight;
    // End of variables declaration//GEN-END:variables

}
